var e = getApp(), t = require("./tmp.js");

Page({
    data: {
        server: e.server,
        serverUrl: e.serverUrl,
        total: 0
    },
    changeAddr: function() {
        var e = "/pages/addr/create/create";
        this.data.addr && (e += "?id=" + this.data.addr.id), wx.navigateTo({
            url: e
        });
    },
    reload: function() {
        var t = this, r = wx.getStorageSync("usr");
        this.setData({
            usr: r
        }), e.request({
            url: e.server + "/userApi/getAddressList",
            method: "POST",
            data: {
                userToken: r.userToken
            },
            header: {
                "content-type": "application/x-www-form-urlencoded"
            },
            success: function(e) {
                if (0 === e.data.code) {
                    var r = e.data.result.list[0];
                    t.setData({
                        addr: r
                    });
                }
                t.checkAddr();
            }
        });
    },
    checkAddr: function() {
        return !!this.data.addr || (wx.showModal({
            content: "您还没有收货地址，是否现在新建一个？",
            confirmText: "新建",
            success: function(e) {
                e.confirm && wx.navigateTo({
                    url: "/pages/addr/create/create"
                });
            }
        }), !1);
    },
    submit: function() {
        if (this.checkAddr()) {
            var t = this.data.addr, r = this.data.products.map(function(e) {
                return {
                    pc_id: e.id,
                    quality: e.quality
                };
            });
            e.request({
                url: e.server + "/orderApi/getOrderID",
                method: "POST",
                data: {
                    userToken: this.data.usr.userToken,
                    address_id: t.id,
                    buyer: t.buyer,
                    mobile: t.mobile,
                    type: 2,
                    products: JSON.stringify(r)
                },
                header: {
                    "content-type": "application/x-www-form-urlencoded"
                },
                success: function(e) {
                    0 === e.data.code && wx.redirectTo({
                        url: "/pages/order/list/list"
                    }), wx.showToast({
                        icon: "none",
                        title: e.data.msg.zh
                    });
                }
            });
        }
    },
    onLoad: function(e) {
        this.setData(t.get()), this.reload();
    },
    onReady: function() {},
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {
        t.set(null);
    },
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {}
});